package com.premedsidekick;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class TextFieldAndSpinnerAdapter extends ArrayAdapter<String> {
	Context context;
	String[] hours;
	String[] names;
	SharedPreferences preferences;
	SharedPreferences.Editor editor;
	String filename;
	EditText edittext;
	Spinner spinner;
	ArrayAdapter<CharSequence> arrayadapter;

	
	public TextFieldAndSpinnerAdapter (Context context, String[] names, 
			String[] hours, String filename) {
		super(context, 0);
		//super(context, R.layout.textview, R.id.selection, names);
		//super (context, R.layout.research_layout, names);
		this.context = context;
		this.filename = filename;
		this.names = names;
		this.hours = hours;
		
		preferences = context.getSharedPreferences(filename, 0);
		editor = preferences.edit();
	}
	
	/*
	public TextFieldAndSpinnerAdapter (Context context, ArrayList<String> list, 
			String[] hours, String filename) {
		super (context, R.layout.research_layout);
		this.list = list;
		this.context = context;
		this.filename = filename;
		this.names = names;
		this.hours = hours;
		
		preferences = context.getSharedPreferences(filename, 0);
		editor = preferences.edit();
	}
	*/
	
	public View getView (int position, View view, ViewGroup parent) {
		View row = super.getView(position, view, parent);
		// here
		//ViewHolder holder = (ViewHolder)row.getTag();
		
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate (R.layout.research_layout, null, true);
		}

		row = super.getView(position, view, parent);
		//edittext = (EditText) view.findViewById (R.id.edittext);
		edittext = (EditText) row.findViewById (R.id.edittext);
		edittext.setOnKeyListener(new OnKeyListener() {
		    public boolean onKey(View v, int keyCode, KeyEvent event) {
		        // If the event is a key-down event on the "enter" button
		        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
		            (keyCode == KeyEvent.KEYCODE_ENTER)) {
		          // Perform action on key press
		          Toast.makeText(context, edittext.getText(), Toast.LENGTH_SHORT).show();
		          return true;
		        }
		        return false;
		    }
		});
		//spinner = (Spinner) view.findViewById (R.id.spinner);
		spinner = (Spinner) row.findViewById (R.id.spinner);
		arrayadapter = ArrayAdapter.createFromResource(
	            context, R.array.hour_options, android.R.layout.simple_spinner_item);
		arrayadapter.setDropDownViewResource (android.R.layout.simple_spinner_item);
		spinner.setAdapter(arrayadapter);
		spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
		
		return view;
	}
	public class MyOnItemSelectedListener implements OnItemSelectedListener {

	    public void onItemSelected(AdapterView<?> parent,
	        View view, int pos, long id) {
	      //Toast.makeText(parent.getContext(), "Hours selected " +
	          //parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
	    }

	    public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
	    }
	}
	public class EditTestAndSpinnerWrapper {
		EditText edittext;
		Spinner spinner;
		
		public void EditTestAndSpinnerWrapper() {
			
		}
	}
	
	

}
